Importing and Exporting Data

Md Zulquar Nain

AMU

February 28, 2024

Importing and Exporting Data

  • In R, we can import data formats like ASCII, Excel, CSV and many more

  • Files with extensions

    • .txt (files in text fomrat),
    • .csv (files in comma seperated values) and
    • .xlxs (files in excel formate)
  • Lets learn one by one

  • Note: For more about file extensions (.txt, .csv, .xlxs) please GOOGLE

  • Two most commonly used methods for importing data

read.csv("path/filename.csv")
read.table("path/filename.txt")
# YOU can assign name to the imported data sets
data <- read.csv("path/filename.csv")
data1 <- read.table("path/filename.txt")
# Here data and data1 is the assigned names to the imported datasets

Importing data using read.csv

  • read.csv is used import data file with .csv extension aka file in which data is stored with comma sepearted value

  • Let’s take an example

# First let's get help
?help(read.csv)
?read.csv
# Above these functions will help you to get the details about importing the .csv files
  • Let’s import/read the data file named example.csv
# First let's get help
mydata <- read.csv("example.csv")

importing data using read.table function

  • Arguments for the read.table

    • header = TRUE read first line as column names

    • dec = "," comma as decimal mark

    • sep = "_" underscore as column separator (“ for tabstop)

    • fill = T fill incomplete rows with NAs at the end

    • skip = 12 ignore the first 12 lines (eg with meta data)

importing data using read.table function

# First let's get help
?help(read.table)
?read.table
# Above these functions will help you to get the details about importing files using read.table
  • Let’s import/read the data file named example.txt
# importing text file
mydata1 <- read.table("example.txt", header=TRUE, sep="\t")

Importing data- Move away from PATH DEPENDENCY

  • In the above two methods, we have to give path of the data file stored/kept

  • Instead, we can use file.choose() function

# Importing csv file
mydata2 <- read.csv(file.choose(),header = T)
# importing text file
mydat3 <- read.table(file.choose(),header = T,sep=",")
# importing tab delimited file
mydata4 <- read.table(file.choose(),header = T,sep = "\t")
# another way to import tab delimited file
mydata5 <- read.delim(file.choose(),header = T,sep = "\t")

Writing/Exporting Data to a file

  • Setup a directory in not done using command setwd()

  • or know your directory using command getwd()

  • If Rproject is being used no need of setting working directory

  • In R, we can write data frames easily to a file, using the write.table() command.

# another way to import tab delimited file
write.table(name, file="name.txt", quote=F)
  • First argument refers to the data frame to be written to the output file

  • the second is the name of the output file.

  • By default R will surround each entry in the output file by quotes, so we use quote=F.

  • Use the below command, notice the difference

# another way to import tab delimited file
write.table(mydata, file="name1.txt", quote=F,row.names=F )

Excel?

  • Importing data from Excel file
require(readxl) # package required to import excel file

mydata6 <- read_excel("example.xlsx",sheet = 1)
  • Exporting data to Excel file
require(writexl)# package required to export data to an excel file

write_xlsx(mydata, "name1.xlsx")

One more method

New method

THANKS